package com.asia.algorithmcode.array;

/**
 * @DESCRIPTION: 80 删除有序数组中的重复项2
 * @USER: wanfu
 * @DATE: 2025/4/1 星期二 11:41
 */
public class RemoveDuplicates {


    public static void main(String[] args) {
        int[] arr = {0,0,1,1,1,1,2,3,3};
        int length = new RemoveDuplicates().removeDuplicates(arr);

        for (int i = 0; i < length; i++) {
            System.out.printf(arr[i] + "\t");
        }
        System.out.println();
    }


    public int removeDuplicates(int[] nums) {
        int length = nums.length;
        int left = 0;
        int right = 0;
        while (right < length) {
            int tempLeft = right;
            while (right < length && nums[right] == nums[tempLeft]) {
                right++;
            }
            boolean needMove = right - tempLeft > 2;
            if (needMove) {
                nums[left++] = nums[tempLeft];
                nums[left++] = nums[tempLeft];
            } else { // 1个或者2个连续，此时 right - left
                if (right - tempLeft <= 1) {
                    nums[left++] = nums[tempLeft];
                } else {
                    nums[left++] = nums[tempLeft];
                    nums[left++] = nums[tempLeft];
                }
            }
        }
        return left;
    }

}
